package com.kl.modular.sysman.system.controller;

import com.kl.common.pager.PageParameter;
import com.kl.common.pager.Pager;
import com.kl.component.shiro.AuthorityUser;
import com.kl.modular.common.controller.BaseController;
import com.kl.modular.sysman.system.constant.ResponsePath;
import com.kl.modular.system.dao.bo.SystemAuthResourceBo;
import com.kl.modular.system.dao.bo.SystemRoleAuthBo;
import com.kl.modular.system.dao.model.SystemAuthResource;
import com.kl.modular.system.dao.model.SystemRoleAuth;
import com.kl.modular.system.service.api.ISystemAuthResourceService;
import com.kl.modular.system.service.api.ISystemRoleAuthService;
import com.kl.modular.system.service.constant.SystemConStant;
import com.kl.modular.system.service.vo.SystemAuthResourceVO;
import com.kl.modular.system.service.constant.SystemConStant;
import com.kl.modular.system.service.vo.SystemAuthResourceVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

;

/**
 * 后台主页
 * @author kl_zjb
 * @time 2016/09/13 15:53
 *
 */
@Controller
public class IndexController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
	private ISystemAuthResourceService systemAuthResourceService;

	@Autowired
	private ISystemRoleAuthService systemRoleAuthService;
	
	
	/**
	 * 后台首页
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/sysman/index/main" , method = RequestMethod.GET)
	public String main(HttpServletRequest request) throws Exception{
		logger.info("主页main>>");
		return ResponsePath.LOGIN_SUCCESS;
	}
	
	/**
	 * 后台头部主页
	 * @return
	 */
	@RequestMapping(value = "/sysman/index/topPanel" , method = RequestMethod.GET)
	public String topPanel(HttpServletRequest request){
		logger.info("后台头部主页>>");
		// 模块导航信息
		AuthorityUser authUser = getAuthorithUser();
		SystemAuthResourceBo systemAuthResourceBo = new SystemAuthResourceBo();
		systemAuthResourceBo.setRoleCodes(authUser.getRoleCodes().split(","));
		systemAuthResourceBo.setRoleShiroCode(authUser.getRoleShiroCode());
		systemAuthResourceBo.setModuleType(getAuthResourceType());
		PageParameter pageParameter = new PageParameter();
		pageParameter.setPageBoolean(PageParameter.PAGE_BOOLEAN_FALSE);
		systemAuthResourceBo.setAuthType(SystemConStant.SYSTEM_AUTH_TYPE_MODULAR);
		List<SystemAuthResource> systemAuthResourcePager = systemAuthResourceService.getListAllByBo(systemAuthResourceBo, pageParameter);
		request.setAttribute("systemAuthModularResource", systemAuthResourcePager);
		request.setAttribute("username", authUser.getUserName());
		request.setAttribute("rolenames", authUser.getRoleNames());
		return ResponsePath.GUESTSTARPOINT_MAN_HEADER;
	}	
	
	/**
	 * 后台左边主页
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/sysman/index/leftPanel" , method = RequestMethod.GET)
	public String leftPanel(String modularAuthCode,HttpServletRequest request) throws Exception{
		logger.info("后台左边主页>>");
		// 加载菜单信息 模块菜单、导航菜单、功能菜单
		AuthorityUser authUser = getAuthorithUser();
		SystemAuthResourceBo systemAuthResourceBo = new SystemAuthResourceBo();
		System.out.println("角色：" + authUser.getRoleCodes());
		systemAuthResourceBo.setRoleCodes(authUser.getRoleCodes().split(","));
		systemAuthResourceBo.setRoleShiroCode(authUser.getRoleShiroCode());
		PageParameter pageParameter = new PageParameter();
		pageParameter.setPageBoolean(PageParameter.PAGE_BOOLEAN_FALSE);
		systemAuthResourceBo.setAuthType(SystemConStant.SYSTEM_AUTH_TYPE_MODULAR);
		systemAuthResourceBo.setModuleType(getAuthResourceType());
		if(!StringUtils.isEmpty(modularAuthCode)){
			systemAuthResourceBo.setAuthCode(modularAuthCode);
		}
		
		Pager<SystemAuthResource> systemAuthResourcePager = systemAuthResourceService.getListByBo(systemAuthResourceBo, pageParameter);
		SystemAuthResourceVO systemAuthResourceVO  = new SystemAuthResourceVO();
		// 默认当前导航菜单
		if(systemAuthResourcePager !=null && (systemAuthResourcePager.getResult()!=null &&systemAuthResourcePager.getResult().size()>0)){
			SystemAuthResource systemAuthResource = systemAuthResourcePager.getResult().get(0);
			if(systemAuthResource!=null && !StringUtils.isEmpty(systemAuthResource.getAuthCode())){
				String roleShiroCode = getAuthorithUser().getRoleShiroCode();
				String roleCodes = getAuthorithUser().getRoleCodes();
				systemAuthResourceVO = systemAuthResourceService.getSystemAuthResourceVOByModularCode(systemAuthResource.getAuthCode(),
						roleShiroCode, roleCodes);
				request.setAttribute("parentAuthResourceMenuName", systemAuthResource.getAuthName());
			}
		}
		request.setAttribute("systemAuthResourceMenu", systemAuthResourceVO);
		return ResponsePath.GUESTSTARPOINT_MAN_LEFTPANEL;
	}	
	
	/**
	 * 后台右边主页/默认工作台
	 * @return
	 */
	@RequestMapping(value = "/sysman/index/rightPanel" , method = RequestMethod.GET)
	public String rightPanel(HttpServletRequest request){
		logger.info("后台右边主页>>");
		return ResponsePath.GUESTSTARPOINT_MAN_RIGHTPANEL_INDEX;
	}	

}
